package com.suixun.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suixun.dao.BannerMapper;
import com.suixun.dao.redis.RedisDao;
import com.suixun.domain.Banner;
import com.suixun.enums.ShowEnum;
import com.suixun.exceptions.SuixunException;
import com.suixun.service.BannerService;
import com.suixun.utils.ListUtil;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jacky
 * @since 2018-01-09
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

	@Resource
	private RedisDao redisDao;

    @Override
	@Cacheable(value = "bannerCache", key = "'banners' + '_' + #index + '_' + #type")
	public List<Banner> listBanners(Integer index, Integer type) {
		EntityWrapper<Banner> wrapper = new EntityWrapper<>();
		if (index != null) {
			wrapper.eq("p_index", index);
		}
		if (type == null) {
			type = 1;
		}
		wrapper.eq("type", type).eq("is_show", ShowEnum.SHOW.getType()).orderBy("p_index");
		return this.selectList(wrapper);
	}

	@Override
	@Cacheable(value = "bannerCache", key = "'banner' + '_' + #index + '_' + #type")
	public Banner getBanner(Integer index, Integer type) {
		EntityWrapper<Banner> wrapper = new EntityWrapper<>();
		wrapper.eq("p_index", index).eq("type", type).eq("is_show", ShowEnum.SHOW.getType());
		return this.selectOne(wrapper);
	}

	@Override
	@Transactional
	public void insertBanner(Banner banner) {
		if (banner.getIsShow() != null && banner.getIsShow() == ShowEnum.SHOW.getType()) {
			// 隐藏该栏位下的所有banner
			hideBanner(banner.getType(), banner.getpIndex());
		}
		this.insert(banner);
		// 清除banner相关的缓存
		redisDao.flushCaches("banner*");
	}

	/**
	 * 功能描述: 隐藏该类型下的banner
	 * 
	 * @param type
	 * @param index
	 */
	private void hideBanner(Integer type, Integer index) {
		Banner banner = new Banner();
		banner.setIsShow(ShowEnum.HIDE.getType());
		Wrapper<Banner> wrapper = new EntityWrapper<>();
		wrapper.eq("type", type);
		wrapper.eq("p_index", index);
		this.update(banner, wrapper);
	}

	@Override
	@Transactional
	public void updateBanner(Banner banner) {

		if (banner.getId() == null) {
			throw new SuixunException("bannerid不能为空");
		}

		if (banner.getIsShow() != null && banner.getIsShow() == ShowEnum.SHOW.getType()) {
			// 获取当前banner
			Banner bannerDo = this.selectById(banner.getId());
			if (bannerDo == null) {
				throw new SuixunException("banner不存在");
			}

			// 隐藏该栏位下的所有banner
			hideBanner(bannerDo.getType(), bannerDo.getpIndex());
		}
		// 修改
		this.updateById(banner);
		// 清缓存
		redisDao.flushCaches("banner*");
	}

	@Override
	public void delete(String bannerIds) {
		if (StringUtils.isBlank(bannerIds)) {
			throw new SuixunException("参数为空");
		}
		List<Integer> list = ListUtil.Integers2List(bannerIds);
		this.deleteBatchIds(list);
		// 清缓存
		redisDao.flushCaches("banner*");
	}

	@Override
	public PageInfo<Banner> pageBanner(Integer page, Integer size, String title, Integer type, Integer index) {
		PageHelper.startPage(page, size);
		Wrapper<Banner> wrapper = new EntityWrapper<>();
		if (StringUtils.isNotBlank(title)) {
			wrapper.like("title", title);
		}
		if (type != null) {
			wrapper.eq("type", type);
		}
		if (index != null) {
			wrapper.eq("p_index", index);
		}
		wrapper.orderBy("is_show desc, create_time desc");
		List<Banner> bannerList = this.selectList(wrapper);
		return new PageInfo<>(bannerList);
	}
}
